package com.gooddare.view 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.ApiLocator;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.gooddare.model.vo.GridviewVO;
	import com.gooddare.view.component.GridViewComponent;
	import com.gooddare.view.component.HomepictureComponent;
	import com.gooddare.view.component.TagsGroupComponent;
	import com.riatimes.mvc.model.vo.DataParamVO;
	import com.riatimes.net.http.HttpService;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import com.greensock.easing.Quad;
	import com.greensock.TweenMax;
	import org.puremvc.as3.interfaces.INotification;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class HomepageFrameMediator extends BaseView
	{
		public static var NAME:String = "HomepageFrameMediator";
		private var homepageFrame:Sprite;
		private var gvc:GridViewComponent;
		private var shape:Sprite;
		private var tagY:int = 0;
		private var tagShape:Sprite;
		private var hpc:HomepictureComponent;
		private var vo:GridviewVO;
		private var arr:Array;
		private var textArr:Array; 
		private var iTxt:TextField;
		private var tTxt:TextField;
		private var line:Sprite;
		public function HomepageFrameMediator(name:String=null,viewComponent:Object=null) 
		{
			if (name != null) NAME = name;
			if (viewComponent == null) viewComponent = new Sprite();
			super(NAME, viewComponent);
			homepageFrame = getView();
			
			ModelLocator.ROOT.addChild(homepageFrame);
			
			var hpVO:DataParamVO = new DataParamVO(ApiLocator.API_HOMEPAGE_JOSN_URL, ApplicationFacade.EVENT_DATA_HOMEPAGE_GET_INFO);
			HttpService.getInstance().getData(ApplicationFacade.getInstance(), hpVO);
		}
		override public function listNotificationInterests():Array {
			return[
				ApplicationFacade.EVENT_APP_QUITSTAGE_CLICK,
				ApplicationFacade.EVENT_DATA_HOMEPAGE_GET_INFO,
				ApplicationFacade.EVENT_DATA_HOMEPIC_POST
			]
		}
		override public function handleNotification(note:INotification):void {
			var data:Object = note.getBody();
			switch(note.getName()) {
				case ApplicationFacade.EVENT_DATA_HOMEPAGE_GET_INFO:
					var vo:GridviewVO = new GridviewVO(data);
					this.vo = vo;
					ModelLocator.gridlistLength = vo.gridlist.length;
					initUI();
					createGroupByGridView(vo);
					createInfo(vo.info);
					createTag(vo);
				break;
				case ApplicationFacade.EVENT_DATA_HOMEPIC_POST:
					if (ModelLocator.hsaPicDispaly == false) {
						hpc = new HomepictureComponent();//实例化图片显示器
						ModelLocator.ROOT.addChild(hpc);
						ModelLocator.hsaPicDispaly = true;
					}
					if (data) {
						hpc.LoadingImage(data.id,data.bimg, data.msg);
					}else {
						var n:int = ModelLocator.playerId;
						var bimg:String = this.vo.gridlist[n].bimg;
						var msg:String = this.vo.gridlist[n].msg;
						hpc.LoadingImage(n,bimg, msg);
					}
				break;
			case ApplicationFacade.EVENT_APP_QUITSTAGE_CLICK:
					quitInfo();
				break;
			}
		}
		private function createTagTitle():void {
			tTxt = new TextField();
			var tfm:TextFormat = new TextFormat();
			tfm.font = "Consolas";
			tfm.color = 0x999999;
			tfm.bold = true;
			tfm.italic = true;
			tfm.size = 18;
			tTxt.defaultTextFormat = tfm;
			tTxt.text = "Tags:";
			line = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_INDEX_LINELLAE_UI) as Sprite;
			line.x = 720+235-75;
			line.y = 140 + tagY + 30-20;
			homepageFrame.addChild(line);
			tTxt.x = 720;
			tTxt.y = 140+tagY+35-20;
			tTxt.autoSize = TextFieldAutoSize.LEFT;
			tTxt.selectable = false;
			homepageFrame.addChild(tTxt);
		}
		private function createTag(vo:GridviewVO):void 
		{
			createTagTitle();
			createTags(vo)
		}
		
		private function createTags(vo:GridviewVO):void 
		{
			tagShape = new Sprite();
			tagShape.x = 720;
			tagShape.y = 140 + tagY + 70-20;
			homepageFrame.addChild(tagShape);
			var tgc:TagsGroupComponent = new TagsGroupComponent(vo);
			tagShape.addChild(tgc);
		}
		
		private function initUI():void 
		{
			iTxt = new TextField();
			var tfm:TextFormat = new TextFormat();
			tfm.font = "Consolas";
			tfm.color = 0x999999;
			tfm.bold = true;
			tfm.italic = true;
			tfm.size = 18;
			iTxt.defaultTextFormat = tfm;
			iTxt.text = "Info:";
			iTxt.x = 720;
			iTxt.y = 110-20;
			iTxt.autoSize = TextFieldAutoSize.LEFT;
			iTxt.selectable = false;
			homepageFrame.addChild(iTxt);
		}
		
		private function createInfo(str:String):void 
		{
			shape = new Sprite();
			shape.x = 720;
			shape.y = 140-20;
			homepageFrame.addChild(shape);
			arr = new Array();
			textArr = new Array();
			
			arr = str.match(/[^*]/g);
			var xPoint:int = 0;
			var yPoint:int = 0;
			var tfm:TextFormat = new TextFormat();
			tfm.color = 0x333333;
			tfm.font = "Arial";
			tfm.size = 12;
			for (var i:uint = 0; i < arr.length; i++ ) {
				var tf:TextField = new TextField();
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.defaultTextFormat = tfm;
				tf.text = arr[i];
				//tf.alpha = 0;
				if (arr[i] == '\n') {//换行
					xPoint = 0;
					yPoint += tf.textHeight + 3;
					this.tagY = yPoint;
				}
				textArr.push(tf);
				shape.addChild(tf);
				TweenMax.to(tf, 0, {alpha:0,x:xPoint + 50, blurFilter: { blurX:52, blurY:2 }} );
				TweenMax.to(tf, 0.5, {delay:i*0.095,x:xPoint,alpha:1, blurFilter: { blurX:0, blurY:0,ease:Quad.easeIn }} );
				//tf.x = xPoint;
				tf.y = yPoint;
				xPoint += tf.textWidth+1;
				if (xPoint > 225) {
					xPoint = 0;
					yPoint += tf.textHeight + 3;
					this.tagY = yPoint;
				}
				tf.selectable = false;
			}		
		}
		private function createGroupByGridView(vo:GridviewVO):void 
		{
			gvc = new GridViewComponent(vo);
			homepageFrame.addChild(gvc);
		}
		
		//退场效果
		private function quitInfo():void {
			facade.removeMediator(NAME);
			TweenMax.killChildTweensOf(homepageFrame, false);
			if (ModelLocator.hsaPicDispaly == true) {
				ModelLocator.ROOT.removeChild(hpc);
				ModelLocator.hsaPicDispaly = false;
			}
			TweenMax.to(iTxt, 0.5, { alpha:0 } );
			TweenMax.to(tTxt, 0.5, { alpha:0 } );
			TweenMax.to(tagShape, 0.5, { alpha:0 } );
			for (var i:uint = 0; i < arr.length; i++ ) {
				TweenMax.to(textArr[i], 0.5, { x:230, alpha:0 } );
			}
			TweenMax.to(line, 0.5, { alpha:0 } );
			gvc.ClearItem();
			TweenMax.delayedCall(1, clearFrame);
		}
		
		public function clearFrame():void {
			TweenMax.killChildTweensOf(homepageFrame, false);
			while (homepageFrame.numChildren) homepageFrame.removeChildAt(0);
			ModelLocator.ROOT.removeChild(homepageFrame);
			
		}
	}

}